<style>
    label.error {
        background: url(<?=base_url()?>assets/img/backgrounds/errortop.png) no-repeat scroll left top #990000;
        color: #FFFFFF;
        display: block;
        font-size: 11px;
        margin: 8px 0 0;
        padding: 6px;
        width: 400px;
    }
    input.error, select.error {
        border: 1px dotted #C53436;
    }
    .ValidationAllErrors {
        background: #FFF;         
        border: 2px solid #990000;
        color: #990000;
        display: block;
        font-size: 11px;
        font-weight: bold;
        margin: 0 0 8px 0;
        padding: 4px;
    }
</style>
<script>
    var delete_answer=false; 
</script>
<?

    if(($form == 'edit')&&(!$validate)){
        foreach($poll as $row){
            $poll_id = $row->id;
            $question = $row->question;
            $multipleselection = $row->multipleselection;
            $ownanswer = $row->ownanswer;
            $randomize = $row->randomize;
            $account_id = $row->account_id;
            $style_id = $row->style_id;
            $stylecustom_id = $row->stylecustom_id;
            $widgetsize = $row->widgetsize;
            $foto_path = $row->foto_path;
        }                                
        foreach($opstions as $row){           
            $pollrepeatvoters_id = $row->pollrepeatvoters_id;
        }
    }
    $style_sel_id= 4;
    if(($form == 'edit')&&(!$validate)){
        if($stylecustom_id!=0){
            $style_sel_id ='---';   //custom style
            $show_custom_picker = TRUE;
        }else { 
            if($style_id!=NULL){
               $style_sel_id = $style_id; 
            }
        }  //edit style
    }
    if($validate){
        if(isset($styleID)){       //default style
            $style_sel_id = $styleID;             
        }else{
            $style_sel_id = '---'; 
        }
        if(isset($stylecustom)){        //custom picker
            $style_sel_id = '---'; 
            $show_custom_picker = TRUE;            
        }
    }             
    if($form == 'new'){
        $style_sel_id = 15;    //new style 
    }    

    // SET PROPERTY FOR DELETE ANSWER FROM DATABASE
    if ($form == 'edit') {
    ?>
    <script>
        delete_answer=true; 
    </script>
    <?}?>
<a id="cc" style="display: none;">Click</a>
<script>
    $('#cc').live('click',function(){
        console.time('validate_form_anketa');
        var result =validate_form_anketa();    
        console.timeEnd('validate_form_anketa');
        alert(result)
        return false;
    })
</script>
<?
    /*if(isset($post)){
        foreach($post as $key => $value){
            echo $key.":".$value.'<br />';                  
        }
    } */
    //echo $form;
?> 
<div id="heading">
    <div id="headinginner">
        <h1><span><?
                    if ($form == 'edit') { 
                        echo $question;
                    }else echo 'Naziv ankete';
                ?>
            </span></h1>

        <p class="clear"></p>
    </div>
    <div id="stage-inner">
        <div id="validation_errors"><?php echo validation_errors('<div class="ValidationAllErrors">', '</div>'); ?></div>
        <?
            if ($form == 'edit') { 
            ?>
            <form action="<?=base_url()?>anketa/uredi_anketu/editpoll" name="editPool" id="editPool" method="post"> 
            <?
            }else{
            ?>
            <form action="<?=base_url()?>anketa/uredi_anketu/createapoll" name="editPool" id="editPool" method="post">
                <?
                }
            ?>                     

            <!--POOL NAME-->
            <div class="box nopadding">
                <div class="forminner">

                    <div style="width: 978px;" class="left">
                        <h4>Pitanje na anketi</h4>
                        <input type="text" value="<? if (($form == 'edit')&&(!$validate)) echo $question; ?><?php echo set_value('question'); ?>" name="question" maxlength="160" style="width: 923px;" class="elmbigred" id="question">
                        <div id="question_error" class="ValidationErrors" style="display: none;"></div>
                        <?php echo form_error('question'); ?>

                        <div class="dividersmall2"></div>
                        <div class="left" id="server-foto" style="display: none;">
                            <h5 class="left" style="font-weight: bold;">Foto: </h5>
                            <span id="f_id" class="left" style="color: #888;"></span>
                            <ul class="navcontrol left">
                                <li style="padding: 0pt 10px 5px;"><a id="delete-server-foto" href="javascript:void(0)" class="single" style="background-position: 9px -328px;">Ukloni fotografiju</a></li>
                            </ul>
                            <p class="clear"></p> 
                        </div>
                        <ul style="padding: 0pt 45px 0pt 0pt;" class="navcontrol right">
                            <li><a class="single linkmedia popup media" href="javascript:void(0)">Dodaj fotografiju</a></li>
                        </ul>

                        <p class="clear"></p>

                    </div>

                    <p class="clear"></p>
                </div> 
            </div>
            <!--END POOL NAME--> 

            <div class="dividersmall"></div>

            <!--POOL ANSWER-->
            <div class="box nopadding">
                <div class="forminner">
                    <div class="answersinsert">

                        <ul id="sortable" class="answers">  
                            <? 
                                if(($form == 'edit')&&(!$validate)){    //EDIT POLL

                                    $maxid;
                                    foreach($questions as $row){
                                        $data['i'] = $row->id;
                                        $data['text'] = $row->text;
                                        echo $this->load->view('anketa/pitanje',$data, TRUE);    
                                        $maxid = $row->id;                           
                                    }

                                ?>
                                <script>var answers = '<?=$maxid+1;?>'</script>
                                <?
                                }else if($validate){      //VALIDATE POLL
                                        foreach($post as $key => $value){
                                            if(substr($key,0,6)=='answer') {
                                                $id=substr($key,7,strlen($key));
                                                $data['i'] = $id;
                                                $data['text'] = $value;
                                                echo $this->load->view('anketa/pitanje',$data, TRUE);    
                                                $maxid = $id;                                                    
                                            }                   
                                    }
                                ?>
                                <script>var answers = '<?=$maxid+1;?>'</script>
                                <?    
                                }else{      //NEW POLL
                                    $maxid = 5;
                                    for($i=1; $i<=$maxid; $i++){ 
                                        $data['i'] = $i;
                                        echo $this->load->view('anketa/pitanje',$data, TRUE);

                                    }
                                ?>
                                <script>var answers = '<?=$maxid+1;?>'</script>
                                <?    
                                }
                            ?>
                        </ul>

                        <div class="answerserror"> 
                            <div class="ValidationErrors" style="width: 400px; margin: 0px 0px 0px 84px; display: none;" id="erroranswertext">Molimo unestite najmanje 2 odgovora.</div>
                        </div>

                        <div class="answerscontrol">
                            <ul class="navcontrol">
                                <li><a class="single linkadd newanswer" href="javascript:void(0)">Dodaj novi odgovor</a></li>
                            </ul>
                            <p class="clear"></p>
                        </div>


                        <div class="qoptions nopadding">    


                            <div class="group">
                                <div class="labelgroup">   
                                    <div class="checkbox">
                                        <input type="checkbox" value="1" id="pMultipleChoice" name="multipleChoice" <?if(($form == 'edit')&&(!$validate))if($multipleselection){echo'checked="checked"';}?><?php if (isset($multipleChoice)){echo'checked="checked"';} ?>>
                                    </div>
                                    <label for="pMultipleChoice">Dozvoli višestruku selekciju</label>
                                    <p class="clear"></p>
                                </div>
                            </div>


                            <div class="group">
                                <div class="labelgroup">   
                                    <div class="checkbox">
                                        <input type="checkbox" value="1" id="pRandom" name="randomiseAnswers" <?if(($form == 'edit')&&(!$validate))if($randomize){echo'checked="checked"';}?><?php if (isset($randomiseAnswers)){echo'checked="checked"';} ?>>
                                    </div>
                                    <label for="pRandom">Nasumični redosled odgovora</label>
                                    <p class="clear"></p>
                                </div>
                            </div>


                            <div class="group noborder">
                                <div class="labelgroup">   
                                    <div class="checkbox">
                                        <input type="checkbox" value="1" id="pOther" name="otherAnswer" <?if(($form == 'edit')&&(!$validate))if($ownanswer){echo'checked="checked"';}?><?php if (isset($otherAnswer)){echo'checked="checked"';} ?>>
                                    </div>
                                    <label for="pOther">Dozvoli korisnicima da unesu sopstveni odgovor</label>
                                    <p class="clear"></p>
                                </div>
                            </div>

                            <p class="clear"></p>

                        </div>
                    </div>

                </div>
            </div> 
            <!--END POOL ANSWER--> 

            <div class="dividersmall"></div>

            <!--POOL STYLES-->
            <div class="box nopadding">
                <div class="forminner">
                    <h4>Izaberite stil ankete</h4>
                    <div class="slider">
                        <div class="regular">

                            <div class="sliderstage" style="overflow: hidden;">

                                <table cellspacing="0" style="margin-left: 0px;">
                                    <tbody>
                                        <tr>
                                            <td> 
                                                <div style="width: 640px;">
                                            <?
                                                foreach($styles as $row){
                                                ?>
                                                
                                                    <div id="<?=$row->id_txt?>" class="item">
                                                        <div class="input">
                                                            <input type="radio" name="styleID" value="<?=$row->id?>" <?if($style_sel_id==$row->id)echo 'CHECKED="CHECKED"'?>>
                                                        </div>
                                                        <label><?=$row->name?></label>
                                                        <a class="img <?=$row->id_txt?>" onclick="return false;" href="#"></a>
                                                    </div>
                                                
                                                <?}?>
                                                </div>
                                            </td> 
                                        </tr>
                                    </tbody>
                                </table>

                            </div>

                            <div class="dividersmall"></div>

                            <div class="itemoptions">
                                <div class="inner">
                                    <div class="title">Izabrtire širinu<br>vašeg widget-a</div>
                                    <div class="option">
                                        <div class="input">
                                            <input type="radio" value="150" disabled="disabled" id="poll_width_150" name="poll_width" <?if(isset($widgetsize)){if($widgetsize==150)echo'checked="checked"';} ?><?php if (isset($poll_width)){if($poll_width==150)echo'checked="checked"';} ?>>
                                        </div>
                                        <label for="poll_width_150">Uzak</label>
                                        <p class="clear"></p>
                                        <p>150px, dobar za sidebar</p>
                                    </div>
                                    <div class="option">
                                        <div class="input">
                                            <input type="radio" value="300" id="poll_width_300" name="poll_width" <?if(isset($widgetsize)){if($widgetsize==300)echo'checked="checked"';}if($form!='edit'){echo'checked="checked"';} ?><?php if (isset($poll_width)){if($poll_width==300)echo'checked="checked"';} ?>>
                                        </div>
                                        <label for="poll_width_300">Srednji</label>
                                        <p class="clear"></p>
                                        <p>300px. dobar za opštu upotrebu</p>
                                    </div>
                                    <div class="option">
                                        <div class="input">
                                            <input type="radio" value="630" id="poll_width_630" name="poll_width" <?if(isset($widgetsize)){if($widgetsize==630)echo'checked="checked"';} ?><?php if (isset($poll_width)){if($poll_width==630)echo'checked="checked"';} ?>>
                                        </div>
                                        <label for="poll_width_630">Širok</label>
                                        <p class="clear"></p>
                                        <p>630px. dobar za blog</p>
                                    </div>
                                    <p class="clear"></p>
                                </div>
                            </div>
                            <div id="poll_width_error" class="ValidationErrors" style="display: none;"></div>

                        </div>
                        <div class="custom">

                            <table cellspacing="0" id="customstyledefault" style="display: table;">
                                <tbody><tr>
                                        <td>
                                            <div id="stylecustom" class="item">
                                                <div class="input">
                                                    <input type="radio">
                                                </div>
                                                <label>Vaš stil</label>
                                                <a class="img customstyle" href="javascript:void(0)"></a>
                                            </div>
                                        </td>
                                    </tr>
                                </tbody>
                            </table>

                            <table cellspacing="0" style="display: none;" id="customstylepicker">
                                <tbody>
                                    <tr>
                                        <td>
                                            <div class="item on">
                                                <div class="input">
                                                    <input type="radio" name="stylecustom" id="stylecustom">
                                                </div>
                                                <label>Vaš stil</label>
                                                <style>
                                                    #for_style label.error{padding: 6px; width: 120px;}
                                                </style>
                                                <div class="customoption" id="for_style">
                                                    <div class="select">
                                                        <select name="customstyleid" id="customstyleid">
                                                            <option value="0">-- Odaberite</option>
                                                            <?
                                                                foreach($styles_custom as $row){
                                                                ?>
                                                                <option value="<?=$row->id?>" <?if(isset($stylecustom_id)){if($stylecustom_id==$row->id){echo'selected="selected"';}}?><?if(isset($customstyleid)){if($customstyleid==$row->id){echo'selected="selected"';}}?>><?=$row->name?></option>
                                                                <?}?> 
                                                        </select>
                                                    </div>
                                                    <div id="customstyle_error" class="ValidationErrors" style="display: none; width: 120px;"></div>
                                                    <div class="control">
                                                        <ul class="navcontrol nopadding">
                                                            <li><a class="linklabel single customstyles" href="<?=base_url()?>style/pogledaj_uredi_stil">Kreiraj novi</a></li>
                                                        </ul>
                                                    </div>
                                                </div>
                                                <a class="customstyle"></a>
                                            </div>
                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                        <p class="clear"></p>
                    </div>
                </div>
            </div>
            <!--END POOL STYLES-->           

            <div class="dividersmall"></div>

            <!--POOL OPTIONS-->
            <div class="box nopadding">
                <div class="forminner">
                    <h4>Opcije ankete</h4>                   

                    <div class="formoptions custompolloptions" style="width: 200px; min-height: 10px;">
                        <div class="inner">
                            <h6 class="left">Blokiraj ponavljane glasove</h6>                    
                        </div>
                    </div>
                    <div class="left" style="width: 732px; text-align: center;">
                        <input type="radio" value="1" id="pBlock_0" name="blockRepeatVotersType" style="margin: 0 2px 0 8px" <?if(isset($pollrepeatvoters_id)){if($pollrepeatvoters_id==1)echo'checked="checked"';} if(!(($form == 'edit')&&(!$validate))){echo'checked="checked"';} ?><?if(isset($blockRepeatVotersType)){if($blockRepeatVotersType==1)echo'checked="checked"';} ?>>                   
                        <label for="pBlock_0">Omogućite duplo glasanje</label>            
                        <input type="radio" value="2" id="pBlock_1" name="blockRepeatVotersType" style="margin: 0 2px 0 8px" <?if(isset($pollrepeatvoters_id)){if($pollrepeatvoters_id==2)echo'checked="checked"';} ?><?if(isset($blockRepeatVotersType)){if($blockRepeatVotersType==2)echo'checked="checked"';} ?>>
                        <label for="pBlock_1">Blokiraj po kukiju (bezbedno rešenje)</label>
                    </div>
                    <p class="clear"></p>
                </div>
            </div>
            <!--END POOL OPTIONS-->

            <div class="divider"></div>

            <!--POOL SUBMIT-->
            <div class="qoptions qoptionssmlbuttons">  
                <div class="right">
                    <input type="hidden" value="<? if($form == 'edit') echo $poll_id;?>" name="poll_id">
                    <input type="hidden" value="<? if(($form == 'edit')&&(!$validate)) echo $foto_path;?><? if(isset($_foto)) echo $_foto;?>" name="_foto" id="_foto">
                    <input type="hidden" value="1" name="custom_validation" />
                    <input type="submit" value="<? if($form == 'edit'){echo 'Uredi anketu';}else echo'Kreiraj anketu';?>" class="button">
                    
                    <p class="clear"></p>
                </div>
                <p class="clear"></p>
            </div>
            <!--END POOL SUBMIT-->
        </form>

        <?
            if(($form == 'edit')&&(!$validate)){
                if($foto_path==""){

                }else{                   
                ?>
                <script>
                    $('#server-foto').css('display','block');
                    $('#f_id').html('<?=$foto_path?>');
                </script>
                <?
                }
            }
        ?>
        <? if((isset($_foto))&&($_foto!='')){
            ?>
            <script>
                $('#server-foto').css('display','block');
                $('#f_id').html('<?=$_foto?>');
            </script>
            <?
            }
        ?>
        <p class="clear"></p>
    </div>
</div>

<script type="text/javascript" charset="utf-8">
    jQuery( document ).ready(function() { 
        $('#delete-server-foto').live('click', function(){
            $('#_foto').val(""); 
            $('#server-foto').fadeOut();           
        })
        $('#close-dialog').live('click',function(){$.modal.close();});        

        jQuery( '.sliderstage .item' ).live( 'click', function(){
            if(this.id=='micro'){
                jQuery("#poll_width_150").attr('disabled',false);    
            }else{
                jQuery("#poll_width_150").attr('disabled',true);    
            }
            var checkbox = '#' + this.id + ' :input';
            jQuery( '.sliderstage :input' ).removeAttr('checked');
            jQuery( '.sliderstage .item' ).attr("class", 'item' );
            jQuery( this ).addClass( 'on' );
            jQuery( checkbox ).attr('checked', 'checked');
            hide_custom_picker();
            return false;
        } );

        jQuery( '#customstyledefault' ).live( 'click', function(){ 
            show_custom_picker();
            return false;
        } );

        //DELETE ANSWER FROM DB
        $('.linkdelete').live('click',function(){
            if(delete_answer){

                if (confirm('Da li želite izbrisati odgovor iz baze')) {
                    var remove = this;
                    var id=remove.id.substr(7,remove.id.length);

                    $.ajax({
                        type: 'POST',
                        url: base_url+'anketa/uredi_anketu/deleteanswer/'+id,
                        dataType: 'json',
                        success: function(data){
                            if(data.success=='failed'){
                                alert('Nešto nije uredi prilikom brisanja.')
                            }else{
                                jQuery(remove).parent().parent().parent().parent().fadeOut('slow',function() {
                                    $(this).empty();
                                });
                            }
                        },
                        error:function(data){alert("Error: " + data);}
                    });                                     
                }      
            }else{
                jQuery(this).parent().parent().parent().parent().fadeOut('slow',function() {
                    jQuery(this).remove();
                });
            }
        })        
    });

    //STYLES CONTROL
    function show_custom_picker() {
        jQuery( '.sliderstage :input' ).removeAttr('checked');
        jQuery( '.sliderstage .item' ).attr("class", 'item' );
        jQuery( '#customstylepicker :input' ).attr('checked', 'true');
        jQuery( '#customstyledefault' ).hide();
        jQuery( '#customstylepicker' ).show();
    }

    function hide_custom_picker() {
        jQuery( '#customstylepicker :input' ).removeAttr('checked');
        jQuery('select[name=customstyleid]').val(0);
        jQuery( '#customstyledefault' ).show();
        jQuery( '#customstylepicker' ).hide();
    }
</script>

<!--SELECT CUSTOM STYLE--> 

<?if(isset($show_custom_picker)){?> 
    <script>
        show_custom_picker();
    </script>
    <?}?>

<div id="media-modal" style="background: #FFFFFF;display: none; padding: 0; height: 170px;">
    <div id="stage">
        <div class="box">
            <div class="forminner" id="for_modal_close">
                <h4>Dialog za prenos fajlova</h4>
                <h6 style="margin: 0 0 30px;">Podržani formati su: *.jpg, *.png, *.gif</h6>

                <div id="file-uploader-form">              
                </div>

                <script>        
                    function createUploader(){            
                        var uploader = new qq.FileUploader({
                            element: document.getElementById('file-uploader-form'),
                            action: base_url+'application/libraries/php.php',
                            debug: true,
                            onComplete: function(id, fileName, responseJSON){
                                var content = '<h4>Dialog za prenos fajlova</h4><h6 style="margin: 0 0 30px;">Prenos uspešno završen</h6><a id="close-dialog" style="background-position: 9px -358px;" class="link_button" href="javascript:void(0)">Zatvori prozor</a><div style="margin: 15px 35px;"></div>';
                                $('#for_modal_close') .html(content);  
                                $('#server-foto').css('display','block');

                                $('#_foto').val(responseJSON.filename);
                                $('#f_id').html($('#_foto').val());                                 
                            }
                        });           
                    }

                    // in your app create uploader as soon as the DOM is ready
                    // don't wait for the window to load  
                    window.onload = createUploader;
                </script>
            </div>
        </div>
    </div>
</div>